//
//  WoFaYunGuiJiVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "WoFaYunGuiJiVC.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "WoFaYunGuiJiModel.h"
#import "WoFaYunGuiJiCell.h"

@interface WoFaYunGuiJiVC ()<MKMapViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) NSArray <WoFaYunGuiJiModel *>*array;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WoFaYunGuiJiVC

-(NSArray<WoFaYunGuiJiModel *> *)array {
    if (!_array) {
        _array = [NSArray array];
//        NSMutableArray *mut = [NSMutableArray array];
//        WoFaYunGuiJiModel *model1 = [WoFaYunGuiJiModel new];
//        model1.city = @"广州市";
//        model1.title = @"客户签收";
//        model1.signuser = @"张三";
//        model1.remarks = @"备注信息";
//        model1.time = @"2020/05/03 10:00";
//        model1.status = @"2";
//        [mut addObject:model1];
//
//        WoFaYunGuiJiModel *model2 = [WoFaYunGuiJiModel new];
//        model2.city = @"广州市";
//        model2.status = @"1";
//        model2.time = @"2020/05/03 10:00";
//        [mut addObject:model2];
//
//        WoFaYunGuiJiModel *model3 = [WoFaYunGuiJiModel new];
//        model3.city = @"广州市";
//        model3.remarks = @"备注信息";
//        model3.time = @"2020/05/03 10:00";
//        model3.status = @"3";
//        [mut addObject:model3];
//
//        _array = mut;
    }
    return _array;
}

-(UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, StatusBarHeight + 7, 44, 44)];
        [_backBtn setImage:[UIImage imageNamed:@"icon_map_back"] forState:0];
        [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)backClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

-(MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _mapView.delegate = self;
    }
    return _mapView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(15, ScreenH * 0.5, ScreenW - 30, ScreenH * 0.5 - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryWoFaYunGuiJi params:@{@"wa_id":self.wa_id,@"type":self.type} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.array = [WoFaYunGuiJiModel mj_objectArrayWithKeyValuesArray:responseObject];
            if (self.array.count) {
                self.array.firstObject.isFirst = YES;
            }
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WoFaYunGuiJiCell *cell = [WoFaYunGuiJiCell WoFaYunGuiJiCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    WoFaYunGuiJiCell *cell = [WoFaYunGuiJiCell WoFaYunGuiJiCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.array.count) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW - 30, 10)];
        view.backgroundColor = HexColor(0xffffff);
        return view;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.array.count) {
        return 10;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (self.array.count) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW - 30, 10)];
        view.backgroundColor = HexColor(0xffffff);
        return view;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.array.count) {
        return 10;
    }
    return 0;
}

@end
